/* pollution_smog_merge.do: This script prepares the dataset used in most of the 
analysis in the paper, by linking the county-day panel of smog check frequencies
with weather and pollution data*/

cd ${path}build

clear all

/* Weather and SO2*/

use ca_weather_county.dta

rename county fips
rename dateNum date

/* The so2 data is the only one with fips codes besides weather, so merge them here*/
merge 1:1 date fips using so2_for_smog.dta, keep(master match using) nogen

egen county = group(fips)

tempfile weather
save `weather'


/* PM10 data*/

use PM10, replace

rename date datestr
gen date = date(date,"MDY")
drop datestr
rename value pm10

tempfile pm10
save `pm10'

/* All the other pollutants*/
use allpollution, replace

gen quarter = qofd(statadate)


rename statadate date

/* Convert to a county-day panel*/

collapse nox ozone co (first) basin district year month day quarter, by(county date)

/* Link to the other datasets*/

joinby county date using `pm10', unm(both)
drop _merge

joinby county date using star_counties, unm(both)

drop _merge

joinby county date using `weather', unm(master)




/*Drop counties that require Smog Checks for change of ownership only*/

drop if inlist(county, 5,8,14,18,22,25,26,32, 53, 55)

/*Create auxiliary date variables*/

replace quarter = qofd(date)
replace month = month(date)
replace year = year(date)

/* Label variables*/
label var fpr_current "Average (Future) FPR Score"
label var N "Total Number of Inspections"

label var directed "Number of Directed Vehicles Inspected"
label var initial_inspection "Number of Initial Inspections Performed"
label var re_pass "Number of Successful Re-Tests"

label var tMin "Daily Low Temperature"
label var tMax "Daily High Temperature"
label var prec "Precipitation"

label values county county
decode county, gen(countyname)
replace countyname = string(county) + " - " + countyname

save ${path}analysis/pollution_star_merged, replace



